package com.looookme.shaozhuo.contactdemo;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener {

    private Object contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initEvent();
    }

    private void initEvent() {
        findViewById(R.id.get_contact).setOnClickListener(this);
        findViewById(R.id.add_contact).setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_contact:
                getContactList();
                break;
            case R.id.add_contact:
                addContact();
                break;
        }
    }

    private void addContact() {
        ContentResolver cr = getContentResolver();
        //向联系人插入数据
        ContentValues cValue = new ContentValues();
        Uri uri = cr.insert(ContactsContract.RawContacts.CONTENT_URI, cValue);
        Long raw_contact_id = ContentUris.parseId(uri);
        cValue.clear();
        //插入联系人
        cValue.put(StructuredName.RAW_CONTACT_ID, raw_contact_id);
        cValue.put(StructuredName.DISPLAY_NAME, "Breath");
        cValue.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        uri = cr.insert(ContactsContract.Data.CONTENT_URI, cValue);
        cValue.clear();
        //插入联系电话
        cValue.put(Phone.RAW_CONTACT_ID, raw_contact_id);
        cValue.put(Phone.NUMBER, "1389746423");
        cValue.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        uri = cr.insert(ContactsContract.Data.CONTENT_URI, cValue);

        Log.i("Info", "插入数据");
    }

    public void getContactList() {
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(Contacts.CONTENT_URI,
                new String[]{Contacts._ID, Contacts.DISPLAY_NAME},
                null,
                null,
                null);
        if (c != null){
            while(c.moveToNext()){
                Log.i("info", "username : " + c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)));
                int id = c.getInt(c.getColumnIndex(Contacts._ID));

                //根据联系人ID查询出联系人的电话号码
                Cursor cc = cr.query(Phone.CONTENT_URI,
                        new String[]{Phone.NUMBER, Phone.TYPE},
                        Phone.CONTACT_ID + "=" + id,
                        null,
                        null);

                if(cc != null){
                    while(cc.moveToNext()){
                        int type = cc.getInt(cc.getColumnIndex(Phone.TYPE));
                        String phone = cc.getString(cc.getColumnIndex(Phone.NUMBER));
                        switch (type){
                            case Phone.TYPE_HOME:
                                Log.i("info", "Phone_Home :" + phone);
                                break;
                            case Phone.TYPE_MOBILE:
                                Log.i("info", "Phone_Mobile :" + phone);
                                break;
                        }
                    }
                    cc.close();
                }

                //根据联系人ID查询出联系人的邮箱地址
                Cursor ccc = cr.query(Email.CONTENT_URI,
                        new String[]{Email.DATA,Email.TYPE},
                        Email._ID + "=" + id,
                        null,
                        null);
                if(ccc != null){
                    while (ccc.moveToNext()){
                        int type = ccc.getInt(ccc.getColumnIndex(Email.TYPE));
                        String email = ccc.getString(ccc.getColumnIndex(Email.DATA));
                        switch (type){
                            case Email.TYPE_HOME:
                                Log.i("info", "Email_Home : " + email);
                                break;
                            case Email.TYPE_WORK:
                                Log.i("info", "Email_Work : " + email);
                                break;
                        }
                    }
                    ccc.close();
                }
            }
            c.close();
        }

        return ;
    }
}
